package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.io.input.ThrottledInputStream;
import org.dromara.system.entity.Category;
import org.dromara.system.entity.Dish;
import org.dromara.system.entity.DishDto;
import org.dromara.system.entity.PageTwoParams;
import org.dromara.system.mapper.CategoryMapper;
import org.dromara.system.mapper.DishMapper;
import org.dromara.system.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyf
 * @description 针对表【dish】的数据库操作Service实现
 * @createDate 2024-09-20 14:56:36
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
    implements DishService {
    @Autowired
    private CategoryMapper categoryMapper;
//    @Autowired
//    private DishMapper dishMapper;

    @Override
    public Page<DishDto> pageLists(PageTwoParams params, String name) {
        Page<Dish> pageInfo = new Page<Dish>(params.getPageNum(),params.getPageSize());
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<Dish>();
        wrapper.like(name!=null,Dish::getName,name);
        wrapper.gt(Dish::getStartTime,"2024-09-20");
        this.page(pageInfo,wrapper);
//        Page<Dish> dishPage = dishMapper.selectPage(pageInfo, wrapper);
        Page<DishDto> dishDtoPage=new Page<>();
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"pageInfo.getRecords()");
        List<Dish> record = pageInfo.getRecords();
        List<DishDto> record2= record.stream().map(
            item->{
                DishDto dto=new DishDto();
                BeanUtils.copyProperties(item,dto);
                //拿到ID,查Id对应的名字
                Long cateId = item.getCategoryId();
                System.out.println("cateId"+cateId);
                Category cate = categoryMapper.selectById(cateId);
                System.out.println("cate"+cate);

                String cateName =cate.getName();
                dto.setCategoryName(cateName);
                return dto;
            }

        ).collect(Collectors.toList());
        dishDtoPage.setRecords(record2);
        return dishDtoPage;
    }

    @Override
    public void add(DishDto dishDto) {
        this.save(dishDto);//只保存基本菜品信息,现在有Id了
    }

    @Override
    public void update(DishDto dishDto) {
        this.updateById(dishDto);
    }

//    public void saveWithFlavor(DishDto dishDto) {
//        //新增菜品,插入菜品口味
//        this.save(dishDto);//只保存基本菜品信息,现在有Id了
//        //获取到菜品Id
//        Long cpid = dishDto.getId();
//        //前端选择的菜品口味list
//        List<DishFlavor> flavor = dishDto.getFlavorList();
//        flavor.stream().map((item) -> {
//            item.setDishId(cpid);
//            dishFlavorDao.insert(item);
//            return item;
//        }).collect(Collectors.toList());
//
////        dishFlavorService.saveBatch(flavor);
//    }

//    public DishDto getByIdWithFlavor(Long id) {
//
//        //分两步查
//        //dish表查
//        Dish dish= this.getById(id);
//        DishDto dishDto=new DishDto();
//        BeanUtils.copyProperties(dish,dishDto);
//        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
//        List<DishFlavor> flavors=dishFlavorDao.selectList(queryWrapper);
//        dishDto.setFlavorList(flavors);
//        //dishFlash查询口味信息
////最后返回的是DishDto对象拷贝
//
//        return dishDto;
//    }
}





